package com.yyyzzz.servlet;

import com.yyyzzz.bean.Admin;
import com.yyyzzz.service.adminService;
import com.yyyzzz.service.impl.adminServiceimpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet( "/adminRegisterServlet")
public class adminRegisterServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
       /* request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");*/
        String adminname = request.getParameter("adminname");
        String password = request.getParameter("password");
        HttpSession session =request.getSession();
        if (adminname == null || "".equals(adminname.trim())){
        session.setAttribute("message","账户不能为空");
        request.getRequestDispatcher("adminRegister.jsp").forward(request,response);
        }
        if (password == null || "".equals(password.trim())){
        session.setAttribute("message","密码不能为空");
        request.getRequestDispatcher("adminRegister.jsp").forward(request,response);

        }
        Admin admin = new Admin();
        admin.setAdminname(adminname);
        admin.setPassword(password);
        //创建adminservice
        adminService adminservice=new adminServiceimpl();
         boolean isRegister=adminservice.isRegister(admin);
        if(isRegister==true){
            response.sendRedirect(request.getContextPath()+"/adminLogin.jsp");
        }else {
            session.setAttribute("message","注册失败");
           response.sendRedirect(request.getContextPath()+"/adminRegister.jsp");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
